# -*- encoding:utf-8 -*-
# @Time: 2022/10/31 21:26
# @Author : moran
# @Email: moran@maqu.com
# @File: tasks.py
# @Software: PyCharm
import logging

from django.core.mail import send_mail
from celery_tasks.celery_main import app
from drfstudy27 import settings

logger = logging.getLogger(__name__)

@app.task(name='send_email_task')
def send_email_task(email, username, password, classes_name):
    """
    发送入学邮件通知
    :param email: 收件人邮箱
    :param username: 用户名
    :param password: 密码
    :param classes_name: 班级名
    :return:
    """
    try:
        email_param = {
            'subject': '码趣教育-PROMOTE系统',
            'message': '',
            'html_message':
                f"""
                    <h2>欢迎加入码趣教育 [{ classes_name }] 进行学习</h2>
                    <h3>诚邀您使用码趣教育-PROMOTE系统</h3>
                    <p>其中包含：题目练习，技术交流，相关书籍学习设备购物商场等</p>
                    <p>您登陆的用户名为：<span style='color:green'>{username}</span></p>
                    <p>您登陆的初始密码为：<span style='color:green'>{password}</span></p>
                """,
            'from_email': settings.EMAIL_HOST_USER,
            'recipient_list': [email]
        }
        res_email = send_mail(**email_param)
    except Exception as e:
        logger.error('入学通知邮件发送[异常],[email: %s, message: %s]'%(email, e))
    else:
        if res_email:
            logger.info('入学通知邮件发送[正常],[email: %s, username: %s]'%(email, username))
        else:
            logger.warning('入学通知邮件发送[失败],[email: %s, username: %s]' % (email, username))


